//
//  PhotoController.m
//  MyTrip
//
//  Created by zhang on 15/9/2.
//  Copyright (c) 2015年 张益. All rights reserved.
//

#import "PhotoController.h"
#import "PhotoCell.h"
#import "AsyncGETConnection.h"
#import "BrowseController.h"

@interface PhotoController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) UICollectionView *photoCollection;
@property (nonatomic, retain) NSMutableArray *photoArr;
@property (nonatomic, retain) NSNumber *start;


@end

@implementation PhotoController

- (void)dealloc
{
    [_photoCollection release];
    [_photoArr release];
    [super dealloc];
}

- (instancetype) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.photoArr = [NSMutableArray array];
        
        
    }
    return self;
}


- (void)createCollection{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(self.view.frame.size.width / 3 - 10 * OffWidth, self.view.frame.size.width / 3 - 10 * OffHeight);
    layout.sectionInset = UIEdgeInsetsMake(5 * OffHeight, 7 * OffWidth, 5 * OffHeight, 7 * OffWidth);
    
    layout.minimumInteritemSpacing = 5 * OffWidth;
    layout.minimumLineSpacing = 5 * OffHeight;
    self.photoCollection = [[[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64 * OffHeight) collectionViewLayout:layout] autorelease];
    _photoCollection.backgroundColor = [UIColor whiteColor];
    _photoCollection.delegate = self;
    _photoCollection.dataSource = self;
    [_photoCollection registerClass:[PhotoCell class] forCellWithReuseIdentifier:@"photo"];
    
    [self.view addSubview:_photoCollection];
    
}

- (void)getMessage{
    
    [AsyncGETConnection asyncGETConnectionWithURLStr:[NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%ld/%ld/photos/?start=0&count=21&gallery_mode=true", self.type, self.idNum] parameters:nil messageBlock:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *items = [dic objectForKey:@"items"];
        [_photoArr addObjectsFromArray:items];
        [_photoCollection reloadData];
    }];
    
}




- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _photoArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    PhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"photo" forIndexPath:indexPath];
    
    NSDictionary *dic = [_photoArr objectAtIndex:indexPath.item];
    [cell setValueWithDic:dic];
    return cell;
    
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    NSDictionary *dic = [_photoArr objectAtIndex:indexPath.item];
//    NSString *imageUrl = [dic objectForKey:@"photo_s"];
    BrowseController *browse = [[BrowseController alloc] init];
    browse.dic = dic;
    [self.navigationController pushViewController:browse animated:YES];
    [browse release];
    
}






- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createCollection];
    [self getMessage];
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
